package io.renren.modules.newBackground.shanghaiCmcc.v2.client;

import com.alibaba.fastjson2.JSON;

import io.renren.modules.newBackground.shanghaiCmcc.v2.manage.DefalutSecurity;
import io.renren.modules.newBackground.shanghaiCmcc.v2.manage.SecurityI;
import io.renren.modules.newBackground.shanghaiCmcc.v2.model.OpenapiResponse;
import io.renren.modules.newBackground.shanghaiCmcc.v2.model.ResponseStatus;
import io.renren.modules.newBackground.shanghaiCmcc.v2.util.ConfSerializeUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;

public class OpenapiHttpCilent {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(OpenapiHttpCilent.class);

    private CloseableHttpClient client = HttpClients.createDefault();
    private String appCode;
    private static String securityUrl;
    private static String openapiUrl;
    private SecurityI securiytManager;

    static {
        //Map<String, String> properties = new ConfSerializeUtil().readConfig("openapi.property");

        //securityUrl = properties.get("security_server_url");
        //openapiUrl = properties.get("openapi_server_url");
        securityUrl = "http://211.136.111.133/open/security";
        openapiUrl = "http://211.136.111.133/open/service";
    }

    /**
     * @param appCode 应用编码
     * @param apk     应用主密钥，在开发者门户中应用管理菜单里获取
     */
    public OpenapiHttpCilent(String appCode, String apk) {
        this.appCode = appCode;
        //init();
        securiytManager = (SecurityI) new DefalutSecurity(securityUrl, appCode, apk);
    }

    /**
     * @param apiCode       能力编码
     * @param transactionId 业务编码
     * @param requestBody   请求
     * @param accessToken   访问令牌
     * @return
     * @throws Exception
     */
    public String call(String apiCode, String transactionId, String requestBody, String accessToken) throws Exception {
        // 1.对报文签名
        String publiceKey = securiytManager.getAsk().getPublicKeyStr();
        Long aedkId = securiytManager.getAedk().getId();
        requestBody = securiytManager.encrypt(requestBody);
        String signValue = securiytManager.sign(requestBody);
        //logger.info("publiceKey:{}, aedkId:{}, requestBody:{}, signValue:{}", publiceKey, aedkId, requestBody, signValue);

        HttpPost httpPost = null;
        CloseableHttpResponse response = null;
        String tmp = null;
        try {
            httpPost = new HttpPost(openapiUrl + "/access");
            httpPost.addHeader("appCode", appCode);
            httpPost.addHeader("apiCode", apiCode);
            httpPost.addHeader("transactionId", transactionId);
            httpPost.addHeader("aedkId", aedkId + "");
            httpPost.addHeader("signValue", signValue);
            httpPost.addHeader("publicKey", publiceKey);
            httpPost.addHeader("accessToken", accessToken);
            httpPost.addHeader("sdkVersion", "sdk.version.2.2");
            httpPost.setEntity(new StringEntity(requestBody, ContentType.create("text/plain", "UTF-8")));
            response = client.execute(httpPost);
            InputStreamReader isr = new InputStreamReader(response.getEntity().getContent(), "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            StringBuffer sb = new StringBuffer();
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            String responseBody = sb.toString();
            //System.out.println(responseBody);
            OpenapiResponse sr = JSON.parseObject(responseBody, OpenapiResponse.class);
            if (ResponseStatus.SUCCESS.toString().equals(sr.getStatus())) {
                String result = sr.getResult();
                if (securiytManager.verify(result, sr.getSignValue(), sr.getPublicKey())) {
                    String otext = securiytManager.decrypt(result);
                    return "{\"status\":\"SUCCESS\",\"result\":\"" + otext.replace("\"", "\\\"") + "\"}";
                } else {
                    return "{\"status\":\"ERROR\",\"errorCode\":\"060101\",\"exceptionCode\":\"sign error\"}";
                }
            } else {
                return responseBody;
            }
        } finally {
            if (response != null) {
                response.close();
            }
            if (httpPost != null) {
                httpPost.abort();
            }
        }
    }

    /**
     * @param apiCode       能力编码
     * @param transactionId 业务编码
     * @param requestBody   请求
     * @return
     * @throws Exception
     */
    public String call(String apiCode, String transactionId, String requestBody) throws Exception {
        return call(apiCode, transactionId, requestBody, null);
    }

    private void init() {
        /*Map<String, String> properties = new ConfSerializeUtil().readConfig("openapi.property");

        securityUrl = properties.get("security_server_url");
        openapiUrl = properties.get("openapi_server_url");*/
        securityUrl = "http://211.136.111.133/open/security";
        openapiUrl = "http://211.136.111.133/open/service";

    }

}
